package com.example.demo.controller;

import com.example.demo.model.entity.Coupon;
import com.example.demo.model.enums.ResultCodeEnum;
import com.example.demo.model.request.coupon.CreateCouponRequest;
import com.example.demo.model.request.coupon.DeleteCouponRequest;
import com.example.demo.model.request.coupon.UpdateCouponRequest;
import com.example.demo.model.response.PageQueryCouponResponse;
import com.example.demo.service.CouponService;
import com.example.demo.util.PageInfo;
import com.example.demo.util.Result;
import com.example.demo.util.ResultGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody CreateCouponRequest request) {
        //参数校验
        if(null == request || StringUtils.isBlank(request.getName()) || null == request.getTotalPrice() || null == request.getDecPrice()) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, false);
        }
        return ResultGenerator.genSuccessResult(couponService.create(request));
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody UpdateCouponRequest request) {
        if(null == request || null == request.getId()) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, false);
        }
        return ResultGenerator.genSuccessResult(couponService.update(request));
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody DeleteCouponRequest request) {
        if(null == request || null == request.getId()) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, false);
        }
        return ResultGenerator.genSuccessResult(couponService.delete(request));
    }

    @GetMapping("/pageQuery")
    public Result<PageQueryCouponResponse> pageQuery(PageInfo pageInfo) {
        //参数校验
        if (pageInfo == null || pageInfo.getCurrent() < 0 || pageInfo.getPageSize() <= 0) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(couponService.pageQuery(pageInfo));
    }

    @GetMapping("/query")
    public Result<Coupon> query(Integer id) {
        if(null == id) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(couponService.query(id));
    }
}
